$:.push File.expand_path(File.dirname(__FILE__))

require 'ws_client'

class HandlerThread < BasicObject
	attr_reader :thread
	
	def singleton_method_added(name)
		::Kernel.puts "method #{name}"
	end

	def initialize
		@queue = ::Queue.new
		
		@thread = ::Thread.new(@queue) do |queue|
			id, args = queue.pop
			::Kernel.puts id, args
			send(id, *args)
		end
	end
	
	def send_message(id, *args)
		@queue << [id, args]
	end
	
	def stop
		send_message(:_exit)
	end
	
	private
	
	def _exit
		Thread.exit
	end
end

class WS < HandlerThread
	def hello(name)
		puts "hello #{name}"
	end
end

#s = WsClient::WebSocket.new('ws://localhost:3000/echo', :origin => 'my_app')
#p 'sending', s.send('hello')
#p 'receiving', s.recv
#s.close

s = TCPSocket.new 'google.dk', 80

Thread.new do
	begin
		p '->'
		p s.recv(1)
		p 'received'
	rescue Exception => err
		p err
	end
end

sleep(1)
s.close
sleep(1)

=begin
s = WsClient::WebSocket.new('ws://localhost:3000/echo', :origin => 'my_app') do |ws|
	ws.onopen {
		puts "Opened"
		p ws.send('Hello')
	}
	
	ws.onmessage { |msg|
		puts "Message received"
		p msg
	}
	
	ws.onclose {
		puts "Closed"
	}
	
	ws.onerror { |reason|
		puts "Error occurred"
		p reason
	}
end

sleep(5)
s.close
=end